package dao;

import entity.ProductCategory;
import entity.Purchase;
import entity.ProductCategory;
import entity.ProductCategory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ProductCategoryDao extends BaseDao {
    //列表所有商品分类信息
    public List getAllProductCategory() throws Exception {
        List productCategoryList = new ArrayList();//创建一个列表对象
        String sql = "select categoryId,categoryName" +
                " from productCategory";//定义sql查询语句
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行查询sql

        //遍历结果集
        while(resultSet.next()){
            String categoryId = resultSet.getString("categoryId");
            String categoryName = resultSet.getString("categoryName");
            ProductCategory productCategory = new ProductCategory(categoryId,categoryName);//构造对象
            productCategoryList.add(productCategory);
        }
        return productCategoryList;//返回列表
    }

    //录入商品分类信息
    public boolean addProductCategory(ProductCategory productCategory) throws Exception {
        String sql = "insert into productCategory(categoryId,categoryName)" +
                " VALUES(?,?)";//定义插入语句
        Connection connection = getConnection();//获取数据库连接
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//创建PreparedStatement
        preparedStatement.setString(1, productCategory.getCategoryId());
        //设置第一个？占位符的值
        preparedStatement.setString(2, productCategory.getCategoryName());
        //设置第二个？占位符的值
        int num = preparedStatement.executeUpdate();//执行sql语句
        if (num==1)
        {
            return true;
        }else {
            return false;
        }
    }

    //修改商品分类信息
    public boolean updateProductCategory(ProductCategory productCategory) {
        int i = 0;
        try{
            Connection connection = getConnection();//获取数据库连接
            String updatesql = "update productCategory set categoryName=?"
                    +" where categoryId=?";//定义sql修改语句
            PreparedStatement preparedStatement = connection.prepareStatement(updatesql);//创建PreparedStatement对象
            preparedStatement.setString(1,productCategory.getCategoryName());
            preparedStatement.setString(2,productCategory.getCategoryId());
            i = preparedStatement.executeUpdate();//执行sql修改语句
        }catch (Exception e){
            e.printStackTrace();
        }
        if (i == 1){
            return true;
        }else {
            return false;
        }
    }

    //删除商品分类
    public String deleteProductCategory(ProductCategory productCategory) throws Exception {
        String result= null;
        Connection connection = getConnection();//获取数据库连接

        String sql = "delete from productCategory where categoryId=?";//定义sql语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,productCategory.getCategoryId());
        int num = preparedStatement.executeUpdate();//执行删除sql操作
        if(num==1){
            //删除成功
            result = "删除成功";
        }else {
            //删除失败
            result = "删除失败";
        }
        return result;
    }


    public static void main(String[] args) throws Exception {
        ProductCategoryDao productCategoryDao = new ProductCategoryDao();
        ProductCategory productCategory = new ProductCategory("999","nidwo ");
        productCategoryDao.updateProductCategory(productCategory);
    }
}
